#pragma once


#include "znx_pool.h"
#include "znx_defs.h"
#include "znx_string.h"
#include "znx_handles.h"
#include "znx_file.h"
#include <yaml.h>

#define znx_null_command    { ZNX_NULL_STRING, NULL, 0 }


static inline void
znx_yaml_parser_delete_handle(void *data)
{
    yaml_parser_delete((yaml_parser_t *)data);
}


static inline void
znx_yaml_document_delete_handle(void *data)
{
    yaml_document_delete((yaml_document_t *)data);
}


typedef struct znx_command_s    znx_command_t;


typedef znx_bool_t(*znx_command_set_handle_t)(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);


struct znx_command_s {
    znx_str_t                   name;
    znx_command_set_handle_t    set_handle;
    size_t                      offset;
};


// If err needs space, apply from the pool.
znx_bool_t znx_yaml_map_node_parser(void *conf,
    znx_pool_t *pool, yaml_document_t *doc,
    yaml_node_t *root, znx_command_t *commands, znx_str_t *err);

znx_bool_t znx_yaml_map_string_parser(void *conf,
    znx_pool_t *pool, znx_str_t *buffer,
    znx_command_t *commands, znx_str_t *err);

// If err needs space, apply from the pool.
znx_bool_t znx_yaml_map_conf_parser(void *conf,
    znx_pool_t *pool, const char *filepath,
    znx_command_t *commands, znx_str_t *err);


znx_bool_t znx_command_set_int64(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_uint64(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_sizet(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_int(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_double(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_bool(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

znx_bool_t znx_command_set_string(
    void *conf, znx_pool_t *pool, znx_command_t *command,
    yaml_document_t *doc, yaml_node_t *node, znx_str_t *err);

